

#include "RigidMeshNode.h"
#include "model.h"
#include <d3dAppForm.h>
extern int TOTALVIDMEM;
//---------------------------------------------------------------------------------------------------------

CRigidMeshNode::CRigidMeshNode() : m_pParentModel(NULL),m_pMatParentWorld(NULL)
{
}

CRigidMeshNode::CRigidMeshNode(CModel* pModel,const int& iType , const int& iIndex) :
m_pParentModel(pModel)
,m_pMatParentWorld(NULL)
{
}

 CRigidMeshNode::~CRigidMeshNode()
{
	Term();
}

 void CRigidMeshNode::Term()
 {
	 if (m_iSerialType == _SER_D3D)
	 {	
		 GXRELEASE(m_pD3DMesh);

		 if (m_pTexture)
		 {
			 m_pTexture->Release();
		 }
	 }
 }

 void CRigidMeshNode::SerialD3D(CIArchive& ar)
 {
	 if (!ar.IsReading())
	 {
		 return;
	 }
	 SerialBase(ar);
	 m_iSerialType = _SER_D3D;

	 HRESULT hr = D3DXCreateMeshFVF(m_uFaceCount,m_uVertexCount,D3DXMESH_MANAGED, 
		 D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1,
		 D3DDEVICE,&m_pD3DMesh);

	 VOID* pVertices;
	 VOID* pIdices;
	 m_pD3DMesh->LockVertexBuffer(NULL,&pVertices);
	 m_pD3DMesh->LockIndexBuffer(NULL,&pIdices);

	 uint32 uVerSize = m_uMeshFVFSize*m_uVertexCount;
	 uint32 uIdSize  = m_uFaceCount*3;

	 ar.Serial((char*)pVertices,uVerSize);
	 ar.Serial((char*)pIdices,uIdSize*sizeof(WORD));

	 m_pD3DMesh->UnlockVertexBuffer();
	 m_pD3DMesh->UnlockIndexBuffer();

 }

 void CRigidMeshNode::SerialMem(CIArchive& ar)
 {
	 if (ar.IsReading())
	 {
		 return;
	 }
	 m_iSerialType = _SER_MEM;

	 m_uMeshFVFSize = D3DXGetFVFVertexSize(m_pD3DMesh->GetFVF());
	 SerialBase(ar);

	 uint32 uVerSize = m_uMeshFVFSize*m_uVertexCount;
	 uint32 uIdSize  = m_uFaceCount*3;

	 VOID* pVertices;
	 VOID* pIdices;
	 m_pD3DMesh->LockVertexBuffer(NULL,&pVertices);
	 m_pD3DMesh->LockIndexBuffer(NULL,&pIdices);

	 ar.Serial((char*)pVertices,uVerSize);
	 ar.Serial((char*)pIdices,uIdSize*sizeof(WORD));

	 m_pD3DMesh->UnlockVertexBuffer();
	 m_pD3DMesh->UnlockIndexBuffer();
 }

 void CRigidMeshNode::SetParentWorldMat()
 {

 }
 void CRigidMeshNode::Update(const float& fElapsedTime)
 {

 }
